package com.archgeek.java.v20240318;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * 二叉树的右视图2021
 * 给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。
 *
 * @author pizhihui
 * @date 2024-03-18 15:30
 */
public class BinaryTreeDemo {

    static class Node {
        int val;
        Node left;
        Node right;
        public Node(int val) {
            this.val = val;
        }
    }


    public void cellTraverse(Node head) {
        if(head == null) {
            return;
        }
        LinkedList<Node> queue = new LinkedList<>();
        queue.add(head);

        while (!queue.isEmpty()) {

            int size = queue.size();
            Node last = queue.getLast();
            System.out.println("last: " + last.val);
            queue.clear();
//            for (int i = 0; i < size; i++) {
//                Node poll = queue.poll();
//                System.out.println(poll.val);
//            }

            if(last.left != null) {
                queue.add(last.left);
//                head = head.left;
            }
            if(last.right != null) {
                queue.add(last.right);
//                head = head.right;
            }

        }

    }


    public static void main(String[] args) {

        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(55);
        head.left.right = new Node(5);
        head.right.right = new Node(4);

        BinaryTreeDemo binaryTreeDemo = new BinaryTreeDemo();
        binaryTreeDemo.cellTraverse(head);


    }

}
